﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;
using TCA_User.Common;
using TCA_User.Tab_Class;

namespace TCA_User
{
    public partial class Bill_List
    {
        private System.Windows.Forms.TabPage Card_Alias_Tab;
        private Gridview_Grp Card_Alias_Table;
        private Gridview_Grp UserName_Alias_Table;

        private TextBox_Lbl Alias_Old_UserName;
        private TextBox_Lbl Alias_New_UserName;
        private Button Change_Name_BT;

        public bool Init_CardAlias_Tab()
        {
            PosSize possize = new PosSize(); ;
            string tab_name = "Alias Manage";
            Card_Alias_Tab = new System.Windows.Forms.TabPage();
            Card_Alias_Tab.Text = tab_name;
            Card_Alias_Tab.SuspendLayout();
            Card_Alias_Tab.Location = new System.Drawing.Point(4, 22);
            Card_Alias_Tab.Size = new System.Drawing.Size(900, 390);
            Card_Alias_Tab.Padding = new System.Windows.Forms.Padding(3);
            Card_Alias_Tab.TabIndex = 3;
            Card_Alias_Tab.UseVisualStyleBackColor = true;
            Card_Alias_Tab.ResumeLayout(true);
            Card_Alias_Tab.PerformLayout();
            this.MainTabControl.Controls.Add(this.Card_Alias_Tab);

            // Init Taxi_TBL
            possize.pos_x = 10;
            possize.pos_y = 6;
            possize.width = 350;
            possize.height = 400;
            Card_Alias_Table = new Gridview_Grp(Card_Alias_Tab,"Card Alias", possize, NO_AUTO_RESIZE,
                                                TCA_Database_Conn, @"SELECT * FROM dbo.[Card_AliasName]", AnchorType.LEFT);

            // Init Taxi_TBL
            possize.pos_x = 370;
            possize.pos_y = 6;
            possize.width = 350;
            possize.height = 400;
            UserName_Alias_Table = new Gridview_Grp(Card_Alias_Tab, "UserName Alias", possize, NO_AUTO_RESIZE,
                                                TCA_Database_Conn, @"SELECT * FROM dbo.[User_AliasName_TBL]", AnchorType.LEFT);

            possize.pos_x = 370;
            possize.pos_y = 420;
            Alias_Old_UserName = new TextBox_Lbl(Card_Alias_Tab, "Old UserName", TextBox_Type.TEXT, possize, AnchorType.LEFT);
            
            possize.pos_x = 370;
            possize.pos_y = 450;
            Alias_New_UserName = new TextBox_Lbl(Card_Alias_Tab, "New UserName", TextBox_Type.TEXT, possize, AnchorType.LEFT);

            Change_Name_BT = new Button();
            Change_Name_BT.Text = "Change Name";
            Card_Alias_Tab.Controls.Add(Change_Name_BT);
            Change_Name_BT.Location = new System.Drawing.Point(370, 480);
            this.Change_Name_BT.Click += new System.EventHandler(this.Change_UserName_BT_Click);


            return true;
        }

        public string Check_Alias_Name(string card_ID)
        {
            string excel_card, system_card;
            foreach (DataRow row in Card_Alias_Table.Data_dtb.Rows)
            {
                excel_card = row["Excel_Name"].ToString().Trim();
                system_card = row["Sytem_Name"].ToString().Trim();
                if (card_ID == excel_card) return system_card;
            }
            return "";
        }

        public string Get_UserName(string login_name)
        {
            string alias_name, user_name;
            foreach (DataRow row in UserName_Alias_Table.Data_dtb.Rows)
            {
                alias_name = row["AliasName"].ToString().Trim();
                user_name = row["UserName"].ToString().Trim();
                if (alias_name == login_name) return user_name;
            }
            return login_name;
        }

        private void Change_UserName_BT_Click(object sender, EventArgs e)
        {
            string old_username, new_username, user_name;
            old_username = Alias_Old_UserName.My_TextBox.Text.Trim();
            new_username = Alias_New_UserName.My_TextBox.Text.Trim();

            // Change Account Tbl
            foreach (DataRow row in Account_TBL_Form.Data_dtb.Rows)
            {
                user_name = row["UserName"].ToString().Trim();
                if (old_username == user_name)
                {
                    MessageBox.Show("Replace in account table. \nChange username: " + old_username + "to " + new_username + "", "Inform");
                    row["UserName"] = new_username;
                    Account_TBL_Form.Submit_BT_Click_event(null, null);
                }
            }

            // Change card_tbl
            foreach (DataRow row in Card_Table_Form.Data_dtb.Rows)
            {
                user_name = row["Card_Owner"].ToString().Trim();
                if (old_username == user_name)
                {
                    row["Card_Owner"] = new_username;
                }
            }
            MessageBox.Show("Replace in Card table. \nChange username: " + old_username + "to " + new_username + "", "Inform");
            Card_Table_Form.Submit_BT_Click_event(null, null);

            // Change card_borrow_tbl
            foreach (DataRow row in Card_Borrow_Tbl.Data_dtb.Rows)
            {
                user_name = row["UserName"].ToString().Trim();
                if (old_username == user_name)
                {
                    row["UserName"] = new_username;
                }
            }
            MessageBox.Show("Replace in Card Borrow table. \nChange username: " + old_username + "to " + new_username + "", "Inform");
            Card_Borrow_Tbl.Submit_BT_Click_event(null, null);
            

            

            return;
 
        }
    }
}